package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.MemberService;
import com.itheima.utils.GetAge;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 会员实现类
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    //根据电话号码查询会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //注册会员
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            member.setPassword(MD5Utils.md5(password));
        }
        member.setRegTime(new Date());
        memberDao.add(member);
    }
    //会员分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {


        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//条件查询
        //将数据给分页插件
        PageHelper.startPage(currentPage,pageSize);

        //查出的数据放在page对象中
        Page<Member> page = memberDao.selectByCondition(queryString);
            //循环查出来的数据
        for (Member member : page) {
            //判断客户是否填写了生日时间
            if (member.getBirthday()!=null){
                //如果填写了,将生日信息交给GetAge工具类处理拿到客户生日
                try {
                    int ageByBirth = GetAge.getAgeByBirth(member.getBirthday());
                    //将客户年龄放入member类
                    member.setAge(ageByBirth);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            //获得健康管理师id
            Integer theHealthManagerId = member.getHealthManagerId();
            //判断客户是否预约了管理师
            if (theHealthManagerId!=null){
                String healthManager = memberDao.findHealthManager(theHealthManagerId);
                member.setHealth(healthManager);
            }
        }
        //将page中的总条数和每页的数据封装进PageResult
        return  new PageResult(page.getTotal(),page.getResult());


    }

    @Override
    public List<User> findUser() {
        return memberDao.findUser();
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
    //删除功能
    @Override
    public void delete(Integer id) {
            //先判断是否和order表有关联的数据，有的话就不可以删除
            int count = memberDao.findCountByMemberId(id);
            if (count > 0) {
                new RuntimeException("当前数据与其他数据关联无法被删除");
            }
            memberDao.deleteById(id);
        }
    }



